package com.wlc.controller;

import com.wlc.pojo.Dept;
import com.wlc.pojo.Result;
import com.wlc.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Slf4j
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/{id}")
    public Result<Dept> getById(@PathVariable Long id) {
        log.info("根据ID查询部门: {}", id);
        try {
            Dept dept = deptService.findById(id);
            return Result.success(dept);
        } catch (Exception e) {
            log.error("查询部门失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<List<Dept>> list() {
        log.info("查询所有部门列表");
        try {
            List<Dept> depts = deptService.findAll();
            return Result.success(depts);
        } catch (Exception e) {
            log.error("查询部门列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/tree")
    public Result<List<Dept>> tree() {
        log.info("查询部门树形结构");
        try {
            List<Dept> deptTree = deptService.findTree();
            return Result.success(deptTree);
        } catch (Exception e) {
            log.error("查询部门树失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PostMapping
    public Result<String> save(@RequestBody Dept dept) {
        log.info("新增部门: {}", dept.getDeptName());
        try {
            deptService.save(dept);
            return Result.success("新增部门成功");
        } catch (Exception e) {
            log.error("新增部门失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PutMapping
    public Result<String> update(@RequestBody Dept dept) {
        log.info("更新部门: {}", dept.getId());
        try {
            deptService.update(dept);
            return Result.success("更新部门成功");
        } catch (Exception e) {
            log.error("更新部门失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除部门: {}", id);
        try {
            deptService.deleteById(id);
            return Result.success("删除部门成功");
        } catch (Exception e) {
            log.error("删除部门失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/status/{id}")
    public Result<String> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        log.info("更新部门状态: id={}, status={}", id, status);
        try {
            deptService.updateStatus(id, status);
            return Result.success("更新部门状态成功");
        } catch (Exception e) {
            log.error("更新部门状态失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}